﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using mal.balades.Map.MapServices;
using mal.balades.Map.Controls;
using mal.balades.Map.ViewModel;
using System.Windows.Browser;

namespace mal.balades.Map
{
    public partial class MainPage : UserControl
    {
        private MapServicesSoapClient service;
        private string visitUrlName;
        private MapLayer layer;     

        public MainPage(string visitUrlName)
        {
            InitializeComponent();

            this.visitUrlName = visitUrlName;          

            string serviceUrl = "Services/MapServices.asmx";
            string host = Application.Current.Host.Source.AbsoluteUri.ToString().Replace("ClientBin/mal.balades.Map.xap", string.Empty);
            string serviceUrlAbsolute = string.Concat(host, serviceUrl);

            service = new MapServicesSoapClient("MapServicesSoap",serviceUrlAbsolute);

            if (!string.IsNullOrEmpty(visitUrlName))
            {
                service.GetVisitForMapCompleted += new EventHandler<GetVisitForMapCompletedEventArgs>(service_GetVisitForMapCompleted);
                service.GetVisitForMapAsync(visitUrlName);
            }
            else
            {
                service.GetAllVisitsForMapCompleted += new EventHandler<GetAllVisitsForMapCompletedEventArgs>(service_GetAllVisitsForMapCompleted);
                service.GetAllVisitsForMapAsync();
            }
        }

        private void InitMap()
        {
            layer = new MapLayer();

            map.Children.Add(layer);
        }

        void service_GetVisitForMapCompleted(object sender, GetVisitForMapCompletedEventArgs e)
        {
            InitMap();
            MapVisitList item = e.Result;
            VisitControl visit = new VisitControl(new VisitViewModel(item));

            if (item.Latitude.HasValue && item.Longitude.HasValue)
            {
                Location location = new Location() { Latitude = item.Latitude.Value, Longitude = item.Longitude.Value };
                //Center the image around the location specified
                PositionOrigin position = PositionOrigin.Center;

                //Add the image to the defined map layer
                layer.AddChild(visit, location, position);

                map.SetView(location, 12);
                
            }
        }

        void service_GetAllVisitsForMapCompleted(object sender, GetAllVisitsForMapCompletedEventArgs e)
        {
            InitMap();

            double minLatitude = double.MaxValue;
            double maxLatitude = double.MinValue;
            double minLongitude = double.MaxValue;
            double maxLongitude = double.MinValue;

            foreach (MapVisitList item in e.Result)
            {

                VisitControl visit = new VisitControl(new VisitViewModel(item));


                Location location = new Location() { Latitude = item.Latitude.Value, Longitude = item.Longitude.Value };
                //Center the image around the location specified
                PositionOrigin position = PositionOrigin.Center;

                //Add the image to the defined map layer
                layer.AddChild(visit, location, position);

                minLatitude = System.Math.Min(minLatitude, item.Latitude.Value);
                maxLatitude = System.Math.Max(maxLatitude, item.Latitude.Value);
                minLongitude = System.Math.Min(minLongitude, item.Longitude.Value);
                maxLongitude = System.Math.Max(maxLongitude, item.Longitude.Value);

            }

            LocationRect rect = new LocationRect(minLatitude, minLongitude, maxLatitude, maxLongitude);
            map.SetView(rect);
            
        }

       
       
    }
}
